package JFrame;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import Tools.Tool;
public class Player {
    public int Player_x;
    public int Player_y;
    public int Player_v_level = 0;
    public int Player_v_vertical = 0;

    public int life = 20;
    public int image_counts=5;
    public boolean invincibility=false;
    public Boolean direction=true;
    public Boolean walk=false;
    public ArrayList<Rectangle> rects=new ArrayList<>();
    public Rectangle rect_player=new Rectangle();
    public long lastKPressTime_jump = 0;
    public long lastKPressTime_bullet = 0;
    public long lastKPressTime_fire = 0;
    public long lastKPressTime_zhen = 0;
    public long lastKPressTime_invincibility=0;
    public String name;
    ArrayList<BufferedImage> IMGS = new ArrayList<>();
    ArrayList<Kill> kills=new ArrayList<>();

    public Player(int x, int y,String name) {
        this.name=name;
        Tool T = new Tool();
        Player_x = x;
        Player_y = y;
        for (int i = 0; i <= 27; i++) {
            try {
                IMGS.add(ImageIO.read(new FileInputStream(T.imgs.get(i))));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }
    }

    public void draw(Graphics g) {
        if(walk) {
            if (direction) {
                if (image_counts >= 13||image_counts<=0) {
                    image_counts = 0;
                }
                g.drawImage(IMGS.get(image_counts++), Player_x, Player_y, null);
            } else {
                if (image_counts >= 27||image_counts<=13) {
                    image_counts = 14;
                }
                g.drawImage(IMGS.get(image_counts++), Player_x, Player_y, null);
            }
        }else {
            g.drawImage(IMGS.get(image_counts), Player_x, Player_y, null);
        }
        rect_player=new Rectangle(Player_x,Player_y,IMGS.get(image_counts).getWidth(),IMGS.get(image_counts).getHeight());
        g.setColor(Color.black);
        g.drawRect(Player_x-8, Player_y-7,20*2,7);
        g.setColor(Color.RED);
        g.fillRect(Player_x-7, Player_y-6,life*2-1,6);
        Font F=new Font("宋体",Font.BOLD,20);
        g.setFont(F);
        g.drawString(name,Player_x-33,Player_y+3);
        for(Kill K:kills) {
            if ((K.x < 0 && K.x > 1080) || K.life <= 0) {
                kills.remove(K);
                break;
            }
            if (K.release) {
                rects.add(new Rectangle(K.x, K.y, K.getwidth(), K.getheight()));
                K.draw(g);
            }
            else {
                g.fillOval(K.x+53,K.y+84,25+85,5);
            }
        }
        if (Player_v_level > 0) {
            if (Player_v_level > 7) {
                Player_v_level = 7;
            }
            Player_x += Player_v_level--;
        } else if (Player_v_level < 0) {
            if (Player_v_level < -7) {
                Player_v_level = -7;
            }
            Player_x += Player_v_level++;
        }
            Player_y -= Player_v_vertical;
            if (Player_y >= 872) {
                Player_y = 872;
                Player_v_vertical = 0;
            } else {
                Player_v_vertical -= 2;
            }
            if(Player_x<0){
                Player_x=0;
            } else if (Player_x>1060) {
                Player_x=1060;
            }

    }
    public void setKills(int x, int y, Boolean direction, int type){
        kills.add(new Kill(x, y, direction, type));
    }

}
